<?php

require_once './common/table.php';


function OneMedal($name, $column)
{
	global $db;

	$sql = "SELECT name,$column FROM users WHERE citizen=14 ORDER BY $column DESC LIMIT 20";
	$result = $db->query($sql);
	
	echo "$name:\n";
	while($res = $result->fetchArray(SQLITE3_NUM)){
		echo "[b]$res[0][/b] ($res[1])\n";
	}
	
	echo "\n\n";
}


function WorkSkill()
{
	global $db;

	$sql = "SELECT name,workskill FROM users WHERE citizen=14 ORDER BY workskill DESC";
	$result = $db->query($sql);
	
	$i = 0;
	$c = array();
	
	$content_news = "";
	$content_raw = "<html><body><pre>\r\n";
	
	while($res = $result->fetchArray(SQLITE3_NUM)) {
		++$i;
		$points = $res[1];
		$level = 6;
		$base = 5000;
		
		if ($points < 5000) {
			break;
		}

		while ($points >= $base*2) {
			$base *= 2;
			$level++;
		}
		
		$percent = ($points - $base) * 100 / $base;
		$content_news .= sprintf("<%d> [b]%s[/b], %d (%.2f%%)\r\n", $i, $res[0], $level, $percent);
		$content_raw .= sprintf("<%d> %s, %d (%.2f%%)\r\n", $i, $res[0], $level, $percent);

		if (!array_key_exists($level, $c)) {
			$c[$level] = 0;
		}
		$c[$level] += 1;
	}
		
	$header = '';
	foreach($c as $level => $num) {
		$header .= sprintf("Level %d: %d\r\n", $level, $num);
	}
	$header .= "\r\n";
	
	$file_news = fopen('./out/work.txt', 'w');
	fwrite($file_news, $header.$content_news);
	fclose($file_news);
	
	$file_raw = fopen('./out/work.html', 'w');
	fwrite($file_raw, "<html><body><pre>\r\n" . $header . $content_raw . "</pre></body></html>");
	fclose($file_raw);
	
}


function Rank()
{
	global $db;

	$sql = "SELECT name,rank FROM users WHERE citizen=14 ORDER BY rank DESC";
	$result = $db->query($sql);
	
	$i = 0;
	
	while($res = $result->fetchArray(SQLITE3_NUM)) {
		++$i;
		$points = $res[1];
		$detail = getRank($points);
		printf("<%d> [b]%s[/b], %s (%.2f%%)\r\n", $i, $res[0], $detail[1], $detail[2]);
	}

}


function Strength()
{
	global $db;

	$sql = "SELECT name,strength FROM users WHERE citizen=14 ORDER BY strength DESC";
	$result = $db->query($sql);
	
	$i = 0;
	
	while($res = $result->fetchArray(SQLITE3_NUM)) {
		++$i;
		$strength = $res[1];
		printf("<%d> [b]%s[/b], %d\r\n", $i, $res[0], $strength);
	}
	
}


function cmp($a, $b)
{
    return ($a["inf"] < $b["inf"]);
}


function DPH()
{
	global $db;
	
	$level_a = 100;
	$level_b = 300;
	$level_s = 550;
	$level_ss = 1000;
		
	$sql = "SELECT name,strength,rank FROM users WHERE citizen=14";
	$result = $db->query($sql);
	
	$c = array();
	$rank_a = 0;
	$rank_b = 0;
	$rank_s = 0;
	$rank_ss = 0;
	
	while($res = $result->fetchArray(SQLITE3_NUM)) {
		$name = $res[0];
		$strength = $res[1];
		$points = $res[2];
		
		$detail = getRank($points);		
		$inf = (($detail[0]-1)/20 + 0.3) * (($strength / 10) + 40);
		$user[] = array('name' => $name, 'strength' => $strength, 'rank' => $detail[1], 'inf' => $inf);
		
		// catagory
		if ($inf >= $level_ss) {
			$rank_ss++;
		} else if ($inf >= $level_s) {
			$rank_s++;
		} else if ($inf >= $level_b) {
			$rank_b++;
		} else if ($inf >= $level_a) {
			$rank_a++;
		}
	
	} // walk through citizen
	
	usort($user, "cmp");
	
	$content_news = '';
	$content_raw = "<html><body><pre>\r\n";
	
	$content_news .= sprintf("SS(>$level_ss): %d\r\n", $rank_ss);
	$content_news .= sprintf("S(>$level_s): %d\r\n", $rank_s);
	$content_news .= sprintf("B(>$level_b): %d\r\n", $rank_b);
	$content_news .= sprintf("A(>$level_a): %d\r\n", $rank_a);
	$content_news .= sprintf("\r\n");

	$content_raw .= $content_news;
	
	foreach($user as $key => $row) {
		$content_news .= sprintf("<%d> [b]%s[/b], %d, %s, %d\r\n",$key+1,$row["name"],$row["strength"],$row["rank"],$row["inf"]);
		$content_raw .= sprintf("<%d> %s, %d, %s, %d\r\n",$key+1,$row["name"],$row["strength"],$row["rank"],$row["inf"]);
	}
	
	$content_raw .= "</pre></body></html>";
	
	$file_news = fopen('./out/dph.txt', 'w');
	fwrite($file_news, $content_news);
	fclose($file_news);
	
	$file_raw = fopen('./out/dph.html', 'w');
	fwrite($file_raw, $content_raw);
	fclose($file_raw);
	
}


// main

$medals = array(
'Hard Worker'=>'m_worker',
'Congress Member'=>'m_congress',
'Country President'=>'m_president',
'Media Mogul'=>'m_media',
'Society Builder'=>'m_builder',
'Resistence Hero'=>'m_rhero',
'Battle Hero'=>'m_bhero',
'Campaign Hero'=>'m_chero',
'Super Soldier'=>'m_soldier',
);

$db = new SQLite3('citizens.db');

//foreach($medals as $name => $column)
//{
//	OneMedal($name, $column);
//}

//Rank($db);
//Strength($db);

WorkSkill();
DPH();

$db->close();

?>
